//
//  AlertIntroView.m
//  CYWFrame
//
//  Created by Macx on 2017/12/13.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "AlertIntroView.h"

@interface AlertIntroView ()

@property (nonatomic, strong) UIView *alertview;
@property (nonatomic, assign) BOOL judge;
@property (nonatomic, strong) NSString *text;

@end

@implementation AlertIntroView

- (id)initWithFrame:(CGRect)frame
{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        //创建遮罩
        UIView *mask = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height)];
        [self addSubview:mask];
        mask.backgroundColor = [UIColor blackColor];
        mask.alpha = 0.3f;
        [mask addTarget:self action:@selector(maskClick)];
        
        //创建alert
        self.alertview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-60, 10)];
        self.alertview.layer.cornerRadius = 5;
        self.alertview.clipsToBounds = YES;
        self.alertview.backgroundColor = [UIColor whiteColor];
        [self addSubview:_alertview];
        [self exChangeOut:self.alertview dur:0.2];
        
        
        
        
    }
    return self;
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    // 第二次就不需要重新创建
    if (self.judge == 0) {
        
        CGFloat width = self.alertview.width;
        
        // title
        UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, width/2, 40)];
        [self.alertview addSubview:title];
        title.text = @"专家简介";
        title.textColor = kGrayColor;
        title.font = kUHSystemFontWithSize(14);
        
        // close
        UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.alertview addSubview:closeBtn];
        [closeBtn setImage:[UIImage imageNamed:@"live_zhubo_close"] forState:UIControlStateNormal];
        closeBtn.frame = CGRectMake(width-40, 0, 40, 40);
        [closeBtn addTarget:self action:@selector(maskClick) forControlEvents:UIControlEventTouchUpInside];
        
        // 分割线
        UIView *line1 = [[UIView alloc] initWithFrame:CGRectMake(0, title.height, width, 1)];
        [self.alertview addSubview:line1];
        line1.backgroundColor = UISeparatedColorRGB;
        
        UILabel *intro = [[UILabel alloc] init];
        [self.alertview addSubview:intro];
        intro.text = self.text;
        intro.numberOfLines = 0;
        intro.font = kUHSystemFontWithSize(12);
        CGFloat introW = self.alertview.width-20;
//        CGSize moodSize = [intro boundingRectWithSize:CGSizeMake(introW, SCREEN_HEIGHT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:intro.font, NSParagraphStyleAttributeName:paragraphStyle, NSKernAttributeName:@1.5f} context:nil].size;
         CGSize moodSize = [intro.text sizeWithFont:kUHSystemFontWithSize(12) constrainedToSize:CGSizeMake(introW, 1000) lineBreakMode:NSLineBreakByWordWrapping];
                intro.frame = CGRectMake(10, 11+line1.y, introW, moodSize.height);
//        intro.frame = CGRectMake(10, 11+line1.y, introW, [LZCTools textSizeWithText:@"xx" font:kUHSystemFontWithSize(12)].height*12);
        
        self.alertview.frame = CGRectMake(0, 0, SCREEN_WIDTH-60, [LZCTools textSizeWithText:@"xx" font:kUHSystemFontWithSize(12)].height*15+20+line1.y);
        self.alertview.center = self.center;
        
    }
    self.judge = YES;
    
}

#pragma mark ==========响应方法==========
// 弹出
-(void)exChangeOut:(UIView *)changeOutView dur:(CFTimeInterval)dur{
    
    CAKeyframeAnimation * animation;
    animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.duration = dur;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    NSMutableArray *values = [NSMutableArray array];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    animation.values = values;
    animation.timingFunction = [CAMediaTimingFunction functionWithName: @"easeInEaseOut"];
    [changeOutView.layer addAnimation:animation forKey:nil];
    
}
- (void)maskClick
{
    // 取消弹框
    [UIView animateWithDuration:0.2 animations:^{
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        self.alertview = nil;
        [self removeFromSuperview];
    }];
}

- (void)initShow:(NSString *)text {
    NSLog(@"可以弹出alert");
    self.text = text;
    if (text.length == 0) {
        self.text = @"当前专家没有简介";
    }
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
